import {
  UPDATE_PRODUCT_LIST
} from '../constants/actionType'
import Immutable from 'immutable'

import Product from '../models/product'

const initialState = Immutable.fromJS({
  count: null,
  productList: Immutable.List()
})

export default (state = initialState, action) => {
  let newState = state
  let productList = Immutable.List()
  switch (action.type) {
    case UPDATE_PRODUCT_LIST:
      const list = action.response.data.resultMsg.product
      console.log('==UPDATE_PRODUCT_LIST==', action, list)
      if (list) {
        if (action.payload.body.startNo !== 0) {
          productList = state.get('productList')
        }
        list.forEach((pro) => {
          productList = productList.push(new Product({
            ...pro
          }))
        })
        newState = newState.set('count', action.response.data.count)
        newState = newState.set('productList', productList)
      }
      // newState = state.set('product', Immutable.fromJS(action.payload.props))
      return newState
    default :
      return state
  }
}
